package cn.test.Async.demo;

import static cn.test.Async.AsyncCall.threadName;

/**
 * create by zsunny
 * data: 2018/8/4
 **/
//利用wait和notifyAll来实现异步回调转同步执行
public class Demo1 extends BaseDemo{

    private final Object lock = new Object();

    @Override //回调相当于 new Runnable...
    public void callback(long response) {
        System.out.println(threadName() +"得到结果");
        System.out.println(threadName() +response);
        System.out.println(threadName() +"调用结束");

        synchronized (lock) { //子线程任务完成后，唤醒主线程
            lock.notifyAll();
        }
    }


    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        System.out.println(threadName() +"====开始");
        demo1.call();//主线程调用call();
        synchronized (demo1.lock){
            try {
                demo1.lock.wait(); //主线程利用wait进行阻塞 //加锁，等待子线程唤醒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(threadName() +"主线程内容");
    }
}
